package com.xqyq.controller.manage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xqyq.service.PrizeService;
import com.xqyq.service.SignService;

@Controller
@RequestMapping("/manage")
public class ManageController {
	@Autowired
	private SignService signService;
	@Autowired
	private PrizeService prizeService;

	@RequestMapping("/api_addPrize")
	@ResponseBody
	public String addPrize(@RequestParam("match_id") int match_id,
			@RequestParam("award_name") String prize_name) {
		int prize_id = prizeService.addPrize(match_id, prize_name);
		return "{\"state\":200,\"award_id\":" + prize_id + "}";
	}

	@RequestMapping("/api_deletePrize")
	@ResponseBody
	public String deletePrize(@RequestParam("award_id") int prize_id) {
		try {
			prizeService.deletePrize(prize_id);
			return "{\"state\":200}";
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"state\":104}";
		}
	}

	@RequestMapping("/api_deleteUser_prize")
	@ResponseBody
	public String deleteUserPrize(@RequestParam("sign_id") int sign_id) {
		try {
			signService.removeUserPrize(sign_id);
			return "{\"state\":200}";
		} catch (Exception e) {
			e.printStackTrace();
			return "{\"state\":104}";
		}
	}

	@RequestMapping("/api_addUser_prize")
	@ResponseBody
	public String api_addUser_prize(@RequestParam("sign_id") String sign_id,
			@RequestParam("award_id") int prize_id) {
		String[] signTemp = sign_id.split(",");
		int[] signList = new int[signTemp.length];
		int id = 0;
		int j = 0;
		for (int i = 0; i < signTemp.length; i++) {
			try {
				id = Integer.parseInt(signTemp[i]);
				signList[j++] = id;
			} catch (Exception e) {
				continue;
			}
		}
		try {
			signService.addUser_prize(signList, prize_id);
			return "{\"state\":200}";
		} catch (Exception e) {
			return "{\"state\":104}";
		}
	}

}
